package com.java.mall_imooc.config;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.lly835.bestpay.config.AliPayConfig;
import com.lly835.bestpay.config.WxPayConfig;
import com.lly835.bestpay.service.BestPayService;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * bestPay配置类
 * @author jiangli
 * @since 2020-03-14 22:26
 */
@Configuration
@AllArgsConstructor
public class BestPayConfig {
    private WxPayProperties wxPayProperties;
    private AliPayProperties aliPayProperties;

    @Bean
    public BestPayService bestPayService() {
        // 微信支付配置
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(wxPayProperties.getAppId());
        wxPayConfig.setMchId(wxPayProperties.getMchId());
        wxPayConfig.setMchKey(wxPayProperties.getMchKey());
        wxPayConfig.setNotifyUrl(wxPayProperties.getNotifyUrl());
        wxPayConfig.setReturnUrl(wxPayProperties.getReturnUrl());

        // 支付宝支付配置
        AliPayConfig aliPayConfig = new AliPayConfig();
        aliPayConfig.setAppId(aliPayProperties.getAppId());
        aliPayConfig.setPrivateKey(aliPayProperties.getMerchantPrivateKey());
        aliPayConfig.setAliPayPublicKey(aliPayProperties.getAlipayPublicKey());
        aliPayConfig.setReturnUrl(aliPayProperties.getReturnUrl());
        aliPayConfig.setNotifyUrl(aliPayProperties.getNotifyUrl());
        //aliPayConfig.setSandbox(true); //是否沙箱

        // 支付类, 所有方法都在这个类里
        BestPayServiceImpl bestPayService = new BestPayServiceImpl();
        bestPayService.setWxPayConfig(wxPayConfig);
        bestPayService.setAliPayConfig(aliPayConfig);
        return bestPayService;
    }

    @Bean
    public Snowflake snowflake() {
        return IdUtil.createSnowflake(1, 1);
    }
}
